﻿/**********************************************************\
*  TinyLib - Strip                                         *
*                                                          *
*  The AnimatedSprite class uses the Strip class           *
*  to handle different rows on a sprite sheet.             *
*                                                          *
* (c) Jens Berglind                                        *
*                                                          *
* Last edited by Jens Berglind 25-06-2012                  *
\**********************************************************/ 

using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace TinyLib.Sprite
{
    public class Strip
    {
        public List<Rectangle> rectangles = new List<Rectangle>();
        public int timeOnChange = 0;
        private int currentTime = 0;
        private int currentIndex = 0;

        public Strip()
        {
        }

        public void Reset()
        {
            currentIndex = 0;
            currentTime = 0;
        }

        public void AddRectangle(Rectangle rectangle)
        {
            rectangles.Add(rectangle);
        }

        public Rectangle GetRectangle(GameTime time)
        {
            int newTime = currentTime + time.ElapsedGameTime.Milliseconds;

            if (newTime >= timeOnChange)
            {
                currentIndex++;
                if (currentIndex > rectangles.Count - 1)
                {
                    currentIndex = 0;
                }
                currentTime = 0;
            }
            else
            {
                currentTime = newTime;
            }

            return rectangles[currentIndex];
        }
    }
}
